package com.zhuhjay.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.zhuhjay.servicebase.exception.GuLiException;
import com.zhuhjay.vod.prop.VodProperties;
import com.zhuhjay.vod.service.VodService;
import com.zhuhjay.vod.utils.InitVodClient;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;
import java.util.StringJoiner;

/**
 * @author ZhuHJay
 */
@Service
public class VodServiceImpl implements VodService {

    @Resource
    private VodProperties properties;

    @Override
    public String uploadVideoAly(MultipartFile file) {
        try {
            //accessKeyId, accessKeySecret
            //fileName：上传文件原始名称
            // 01.03.09.mp4
            String fileName = file.getOriginalFilename();
            //title：上传之后显示名称
            String title = fileName.substring(0, fileName.lastIndexOf("."));
            //inputStream：上传文件输入流
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(
                    properties.getKeyId(), properties.getKeySecret(),
                    title, fileName, inputStream);

            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);

            return response.getVideoId();
        }catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void removeVideoAly(String videoId) {
        try {
            DefaultAcsClient client = InitVodClient.initVodClient(properties.getKeyId(), properties.getKeySecret());
            DeleteVideoRequest request = new DeleteVideoRequest();
            //支持传入多个视频ID，多个用逗号分隔
            request.setVideoIds(videoId);
            // 执行删除
            client.getAcsResponse(request);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuLiException(20001, "视频删除失败");
        }
    }

    @Override
    public void removeAllVideoAly(List<String> videoList) {
        try {
            DefaultAcsClient client = InitVodClient.initVodClient(properties.getKeyId(), properties.getKeySecret());
            DeleteVideoRequest request = new DeleteVideoRequest();
            //支持传入多个视频ID，多个用逗号分隔
            request.setVideoIds(StringUtil.join(videoList.toArray(), ","));
            // 执行删除
            client.getAcsResponse(request);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuLiException(20001, "视频批量删除失败");
        }
    }
}
